package nccu.webapp.controller;
 
import org.apache.commons.lang.StringUtils;
import nccu.service.GenericManager;
import nccu.model.AccountCode;
import nccu.webapp.controller.BaseFormController;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;
 
@Controller
@RequestMapping("/accountcodeform*")
public class AccountCodeFormController extends BaseFormController {

    private GenericManager<AccountCode, Long> accountCodeManager = null;
 
    @Autowired
    public void setAccountCodeManager(@Qualifier("accountCodeManager") GenericManager<AccountCode, Long> accountCodeManager) {
        this.accountCodeManager = accountCodeManager;
    }
 
    public AccountCodeFormController() {
        setCancelView("redirect:accountcodes");
        setSuccessView("redirect:accountcodes");
    }
 
    @ModelAttribute
    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST})
    protected AccountCode showForm(HttpServletRequest request)
    throws Exception {
        String id = request.getParameter("id");
 
        if (!StringUtils.isBlank(id)) {
            return accountCodeManager.get(new Long(id));
        }
 
        return new AccountCode();
    }
 
    @RequestMapping(method = RequestMethod.POST)
    public String onSubmit(AccountCode code, BindingResult errors, HttpServletRequest request,
                           HttpServletResponse response)
    throws Exception {
        if (request.getParameter("cancel") != null) {
            return getCancelView();
        }
 
        if (validator != null) { // validator is null during testing
            validator.validate(code, errors);
 
            if (errors.hasErrors() && request.getParameter("delete") == null) { // don't validate when deleting
                return "accountcodeform";
            }
        }
 
        log.debug("entering 'onSubmit' method...");
 
        boolean isNew = (code.getId() == null);
        String success = getSuccessView();
        Locale locale = request.getLocale();
 
        if (request.getParameter("delete") != null) {
            accountCodeManager.remove(code.getId());
            saveMessage(request, getText("accountcode.deleted", locale));
        } else {
            accountCodeManager.save(code);
            String key = (isNew) ? "accountcode.added" : "accountcode.updated";
            saveMessage(request, getText(key, locale));
 
            if (!isNew) {
                success = "redirect:accountcodeform?id=" + code.getId();
            }
        }
 
        return success;
    }
}